package com.zhai.domain.activity.service.quota;

import com.zhai.domain.activity.model.aggregate.CreateQuotaOrderAggregate;
import com.zhai.domain.activity.model.entity.*;
import com.zhai.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import com.zhai.domain.activity.model.valobj.OrderStateVO;
import com.zhai.domain.activity.repository.IActivityRepository;
import com.zhai.domain.activity.service.IRaffleActivitySkuStockService;
import com.zhai.domain.activity.service.quota.rule.factory.DefaultActivityChainFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @ClassName: RaffleActivityService
 * @Author: zhaiyongxin
 * @Date: 2025/2/19 17:50
 * @Version: 1.0
 */
@Service
@Slf4j
public class RaffleActivityAccountQuotaService extends AbstractRaffleActivityAccountQuota implements IRaffleActivitySkuStockService {

    public RaffleActivityAccountQuotaService(IActivityRepository activityRepository, DefaultActivityChainFactory defaultActivityChainFactory) {
        super(activityRepository, defaultActivityChainFactory);
    }

    /**
     * 保存订单
     *
     * @param createQuotaOrderAggregate
     */
    @Override
    protected void doSaveOrder(CreateQuotaOrderAggregate createQuotaOrderAggregate) {
        repository.doSaveOrder(createQuotaOrderAggregate);

    }

    /**
     * 构建下单聚合对象
     *
     * @param skuRechargeEntity
     * @param activitySkuEntity
     * @param activityEntity
     * @param activityCountEntity
     * @return
     */
    @Override
    protected CreateQuotaOrderAggregate buildOrderAggregate(SkuRechargeEntity skuRechargeEntity, ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        ActivityOrderEntity activityOrderEntity = ActivityOrderEntity.builder()
                .activityId(activityEntity.getActivityId())
                .userId(skuRechargeEntity.getUserId())
                .activityName(activityEntity.getActivityName())
                .strategyId(activityEntity.getStrategyId())
                .orderId(RandomStringUtils.randomNumeric(12))
                .orderTime(new Date())
                .totalCount(activityCountEntity.getTotalCount())
                .monthCount(activityCountEntity.getMonthCount())
                .dayCount(activityCountEntity.getDayCount())
                .state(OrderStateVO.completed)
                .outBusinessNo(skuRechargeEntity.getOutBusinessNo())
                .sku(skuRechargeEntity.getSku())
                .build();
        CreateQuotaOrderAggregate createQuotaOrderAggregate = CreateQuotaOrderAggregate.builder()
                .activityId(activityEntity.getActivityId())
                .userId(skuRechargeEntity.getUserId())
                .totalCount(activityCountEntity.getTotalCount())
                .monthCount(activityCountEntity.getMonthCount())
                .dayCount(activityCountEntity.getDayCount())
                .activityOrderEntity(activityOrderEntity)
                .build();
        return createQuotaOrderAggregate;
    }

    /**
     * 获取队列值
     *
     * @return
     */
    @Override
    public ActivitySkuStockKeyVO takeQueueValue(Long sku) {
        return repository.takeQueueValue(sku);
    }

    @Override
    public void updateActivitySkuStock(ActivitySkuStockKeyVO activitySkuStockKeyVO) {
        repository.updateActivitySkuStock(activitySkuStockKeyVO);
    }

    /**
     * 清空sku对应库存
     *
     * @param sku
     */
    @Override
    public void clearActivitySkuStock(Long sku) {
        repository.clearActivitySkuStock(sku);
    }

    @Override
    public void clearQueueValue() {
        this.clearQueueValue(null);
    }

    @Override
    public void clearQueueValue(Long sku) {
        repository.clearQueueValue(sku);
    }

    @Override
    public List<Long> querySkuStockList() {
        return repository.querySkuStockList();
    }

    @Override
    public Integer queryRaffleActivityAccountDayPartakeCount(Long activityId, String userId) {
        return repository.queryRaffleActivityAccountDayPartakeCount(activityId, userId);
    }

    /**
     * 查询用户抽奖总次数
     *
     * @param activityId
     * @param userId
     * @return
     */
    @Override
    public Integer queryRaffleActivityAccountPartakeCount(Long activityId, String userId) {
        return repository.queryRaffleActivityAccountTotalPartakeCount(activityId, userId);
    }
}
